var screenWidth = $(window).width();
var screenHeight = $(window).height();
//时间格式化
Date.prototype.Format = function (fmt) { //author: meizz
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

var params = {
  staffNum : getQueryString("staffNum"),
  drawCode : 0,            //抽奖code
  storeNo : "" ,           //店铺编号
  storeAddress :[],       //店铺详细地址
  selectEmptyAddr : false,     //判断是否要填收货地址接口
  emptyAddrList : []       //未填地址数据
}

function _onorientationchange(e){
  if(window.orientation==90||window.orientation==-90){
    $("#landscape_wrap").css("display", "-webkit-box");  //显示竖屏浏览提示框
    document.getElementById('viewport').content = 'width=750,user-scalable=no,target-densitydpi=' +
    (750 / parseInt(window.screen.width) * window.devicePixelRatio * 160);
  }else{
    //竖屏下恢复默认显示效果
    $("#landscape_wrap").css("display", "none");
    document.getElementById('viewport').content = 'width=750,user-scalable=no,target-densitydpi=' +
    (750 / parseInt(window.screen.width) * window.devicePixelRatio * 160);
  }
  _resize(e);
}

var cfg = {
  isBegin: false, // 是否在游戏中
  num: 0, // 抽奖机会
  getPrize: ""
};

var prize = [
  {id: 0, productCode : 0, name: "享优乐电动牙刷（商务装）", img: "images/prize01.png", lc : "袋"},
  {id: 1, productCode : 1,name: "空气净化器", img: "images/prize02.png" ,lc : "台"},
  {id: 2, productCode : 2,name: "华为P10 Plus手机", img: "images/prize03.png" , lc :"个"},
  {id: 3, productCode : 3,name: "萃雅菁颜无痕体验装", img: "images/prize04.png",lc:"个"},
  {id: 4, productCode : 4,name: "萃雅密集修护精华体验装", img: "images/prize05.png",lc:"盒"}
];

var productCodeObj = {
  "20203969" : 0,
  "20601170" : 1,
  "20601171" : 2,
  "20601172" : 3,
  "20601173" : 4
}

var prizeObj = {
  "0" : ["享优乐电动牙刷（商务装）","images/prize01.png"],
  "1" : ["空气净化器","images/prize02.png"],
  "2" : ["华为P10 Plus手机","images/prize03.png"],
  "3" : ["萃雅菁颜无痕体验装","images/prize04.png"],
  "4" : ["萃雅密集修护精华体验装","images/prize05.png"],
}

$(function() {
  var ua = navigator.userAgent.toLowerCase();
  //如果不是微信浏览器
  if(ua.match(/MicroMessenger/i) !="micromessenger") {
    $("#landscape_wrap_wx").css("display", "-webkit-box");  //显示竖屏浏览提示框
  }else{
    if(ua.indexOf("/zh_cn") == -1){
      $("#landscape_wrap_mobile").css("display", "-webkit-box");  //显示竖屏浏览提示框
    }
  }

  if(window.orientation==90||window.orientation==-90){
    $("#landscape_wrap").css("display", "-webkit-box");  //显示竖屏浏览提示框
    document.getElementById('viewport').content = 'width=750,user-scalable=no,target-densitydpi=' +
        (750 / parseInt(window.screen.width) * window.devicePixelRatio * 160);
  }
  window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize",     function(e){_onorientationchange(e);}, false);
  'use strict'; // 严格模式
  //获取抽奖记录
  function selectWinRecordByDealerNo(){
    $.ajax({
      url : system.urls + 'httpTranGet.do?env='+system.environmentStr+'&url=awardYounger%2FselectWinRecordByDealerNo%3FdealerNo%3D'+params.staffNum,
      dataType: 'json',
      success: function(data,status,xhr){
        if(data.result == 200 && data.data.length !=0){
          var datas = data.data,
              newData = [],         //临时组合抽奖记录
              sta = {},           //单条组合抽奖记录
              prizeNum = "",
              dataObj = [];
          for(var i=0;i<datas.length;i++){
            if(datas[i].isWin == 1){
              prizeNum = prizeObj[productCodeObj[datas[i].productCode]];
              dataObj = new Date(datas[i].winTime+7776000000).Format("yyyy年MM月dd日");;
              sta = {
                name : prizeNum[0],
                img : prizeNum[1],
                address : datas[i].storeNo?datas[i].storeNo:"对不起，活动已截止， 奖品已失效，下次请及时提交领奖地址哦！",
                time : dataObj,
                status :  datas[i].status,
                phone : datas[i].promCode
              }
              newData.push(sta);
              sta = {};
            }else{
              sta = {
                name : "未中奖",
                img : "",
                address : "",
                time : "",
                status :  "",
                phone : ""
              }
              newData.push(sta);
              sta = {};
            }
          }

          //向模板中插入数据
          var data = {
            list: newData
          };
          var html = template("recordList", data);
          document.getElementById("swiperList").innerHTML = html;


          var oSwiper, iSwiper;
          oSwiper = new Swiper('#o-c',{
            direction : 'vertical',
            mousewheelControl: true,
            initialSlide: 0,
            resistanceRatio : 0,
            resistance : true,
            onSlideChangeStart: function() {
              iSwiper.update();
            }
          });

          iSwiper = new Swiper('#i-c', {
            direction : 'vertical',
            slidesPerView: 'auto',
            nested: true,
            freeMode: true,
            scrollbar:'.swiper-scrollbar',
            resistanceRatio : 0,
            resistance : true,
            scrollbarDraggable : true
          });
        }else{
          $(".no-record").show();
        }
      }
    })
  }

  $.ajax({
    url : system.urls + 'queryWXJSconfig.do',
    data : {
      env : system.environmentStr,
      url : location.href
    },
    dataType: 'json',
    async: false,
    success: function(data) {
      var datas = JSON.parse(data.data);
      wx.config({
        debug: false,
        appId: datas.appId,
        timestamp: datas.timestamp,
        nonceStr: datas.nonceStr,
        signature: datas.signature,
        jsApiList:  [
          'hideOptionMenu',
          'showOptionMenu',
          'onMenuShareTimeline',
          'onMenuShareAppMessage',
          'onMenuShareQQ',
          'onMenuShareWeibo',
          'closeWindow',
          'addCard',
          'chooseCard',
          'openCard',
          'hideMenuItems',
          'previewImage'
        ]
      });
    }
  })

  wx.ready(function (){
    wx.hideOptionMenu();
  })

    selectWinRecordByDealerNo();
});